//
//  gongju.m
//  afn
//
//  Created by mac on 2018/4/13.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "gongju.h"
#import "ZKImgRunLoopView.h"//轮播器

@implementation gongju

+(void)AFNPOSTDataUrl:(NSString *)url
              andDic:(NSDictionary *)dic
        andTableView:(UITableView *)tableview
             andView:(UIView *)view
     andSuccessBlock:(void(^)(id responseObject))responseObjectBlock
       anderrorBlock:(void(^)(void))errorBlock{
    
    //如果正在上下拉刷新 就不显示
    if([tableview.mj_header isRefreshing]==NO&&[tableview.mj_footer isRefreshing]==NO)
    {
        
        if(view!=nil)
        {
            
            [MBProgressHUD showHUDAddedTo:view animated:YES];
        }
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    


    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];

    [manager POST:[NSString stringWithFormat:@"%@",url]  parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //打印接口地址和参数 和返回的参数
            NSLog(@"\n接口调用成功___\n根地址___%@\n接口:%@ \n参数:%@  \n返回的参数%@",_addressUrl,url,dic,[[responseObject description] kdtk_stringByReplaceingUnicode]);
            
          
            
            [MBProgressHUD hideHUDForView:view animated:YES];
            
            [tableview.mj_header endRefreshing];
            
            [tableview.mj_footer endRefreshing];
            
            BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
            
            NSLog(@"%d",dataModel.status);
            if (dataModel.status) {
                if(responseObjectBlock!=nil)
                {
                    
                    responseObjectBlock(dataModel.result);
                }
            }
            else{
                [gongju svpInfo:dataModel.code];
                return ;
            }
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            
            [MBProgressHUD hideHUDForView:view animated:YES];
            
            [tableview.mj_header endRefreshing];
            
            [tableview.mj_footer endRefreshing];
            
            //打印接口地址和参数 和返回的参数
            NSLog(@"\n报错___\n根地址___%@\n接口:%@ \n参数:%@  \n返回的失败原因%@",_addressUrl,url,dic,error);
            
            
            [FTToastIndicator dismiss];
            
            [gongju svpInfo:@"网络连接失败"];
            
            if(errorBlock!=nil)
            {
                errorBlock();
            }
            
        });
    }];
    
}

+(void)AFNGETDataUrl:(NSString *)url
              andDic:(NSDictionary *)dic
        andTableView:(UITableView *)tableview
             andView:(UIView *)view
     andSuccessBlock:(void(^)(id responseObject))responseObjectBlock
       anderrorBlock:(void(^)(void))errorBlock{
    
    //如果正在上下拉刷新 就不显示
    if([tableview.mj_header isRefreshing]==NO&&[tableview.mj_footer isRefreshing]==NO)
    {
        
        if(view!=nil)
        {
            
            [MBProgressHUD showHUDAddedTo:view animated:YES];
        }
    }
    
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    
    [manager GET:[NSString stringWithFormat:@"%@",url]  parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //打印接口地址和参数 和返回的参数
            NSLog(@"\n接口调用成功___\n根地址___%@\n接口:%@ \n参数:%@  \n返回的参数%@",_addressUrl,url,dic,[[responseObject description] kdtk_stringByReplaceingUnicode]);
            
            [MBProgressHUD hideHUDForView:view animated:YES];
            
            [tableview.mj_header endRefreshing];
            
            [tableview.mj_footer endRefreshing];
            
            BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
            
            if (dataModel.status) {
                if(responseObjectBlock!=nil)
                {
                    
                    responseObjectBlock(dataModel.result);
                }
            }
//            else{
//                [gongju svpInfo:dataModel.code];
//                return ;
//            }
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:view animated:YES];
            
            [tableview.mj_header endRefreshing];
            
            [tableview.mj_footer endRefreshing];
            
            //打印接口地址和参数 和返回的参数
            NSLog(@"\n报错___\n根地址___%@\n接口:%@ \n参数:%@  \n返回的失败原因%@",_addressUrl,url,dic,error);
            
            
            [FTToastIndicator dismiss];
            
            [gongju svpInfo:@"网络连接失败"];
            
            if(errorBlock!=nil)
            {
                errorBlock();
            }
            
        });
    }];
}


+ (void)getHederImage :(UIViewContentMode )contentMode :(CGRect)rect :(NSArray*)imageUrlArr :(CGFloat)fenYeheight :(UIView *)rootView :(void(^)(int value)) block
{
    
    //网络图片happendTime
    //网络图片
    
    
    
    
    ZKImgRunLoopView *imgRunView = [[ZKImgRunLoopView alloc] initWithFrame:rect fenYeheight:fenYeheight placeholderImg:_zhanweiImageData];
    imgRunView.imgUrlArray =imageUrlArr;
    [rootView addSubview:imgRunView];
    
    imgRunView.contentMode=contentMode;
    [imgRunView  touchImageIndexBlock:^(NSInteger index) {
        
        if(block!=nil)
        {
            block((int)index);
        }
    }];
    
}


#pragma mark————————svp提醒
+ (void)svpInfo:(NSString *)title
{
    Toast *toast = [Toast toast]; //创建一个toast
    [toast makeText:title]; //显示
}

+(NSString*)encodeString:(NSString*)unencodedString{
    
    NSString *str=@"";
    NSArray *array = [unencodedString componentsSeparatedByString:@" "];
    for (int i=0;i<array.count; i++) {
        if (i==0) {
            str= [array[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        else{
            str=[NSString stringWithFormat:@"%@+%@",str,[array[i]  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        }
    }
    
    return str;
}

//判断字符是否为空
+(BOOL)charIsNil:(NSString *)str{
    
    if ([str isKindOfClass:[NSNull class]]||[str isEqualToString:@""]||str==nil) {
        return YES;
    }
    return  NO;
}

#pragma mark————————————————得到当前时间搓 转 时间搓
+ (NSString *)NSDateToTimeCuo
{
    
    NSDate *date=[NSDate new];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
    
    return timeSp;
}
#pragma mark———————————————— 计算文字宽度
+ (CGSize)getNSStringWeight:(NSString *)string andViewWight:(CGFloat)height andFont:(NSInteger)font
{
    
    
    return  [string sizeOfTextWithMaxSize:CGSizeMake(MAXFLOAT, height) font:[UIFont systemFontOfSize:font]];
    
}

#pragma mark———————————————— 计算文字高度
+ (CGSize)getNSStringSize:(NSString *)string andViewWight:(CGFloat)weight andFont:(NSInteger)font
{
    
    
    return  [string sizeOfTextWithMaxSize:CGSizeMake(weight, MAXFLOAT) font:[UIFont systemFontOfSize:font]];
    
}
#pragma mark———————————————— 跳转 push
+ (void)pushNextViewController:(UIViewController *)nextViewController andRootController:(UIViewController *)rootVc
{
    
    
    rootVc.hidesBottomBarWhenPushed = YES;
    
    [nextViewController.navigationController pushViewController:rootVc
                                                       animated:YES];
    
}

#pragma mark——————————————获取当前版本号
+ (NSString *)getAPPBanben
{
    
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

#pragma mark——————————————网络判断
+(void)monitorNetworking
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            NSLog(@"有网");
        }else{
            SGAlertView *alertView = [SGAlertView alertViewWithTitle:@"⚠️ 警告" delegate:nil contentTitle:@"请去-> [设置 - 蜂窝移动网络 ] 开启数据" alertViewBottomViewType:(SGAlertViewBottomViewTypeOne)];
            [alertView show];
        }
    }];
}
@end
